package com.jiantong.work.impl.service;

import com.google.common.base.Throwables;
import com.google.common.collect.Maps;
import com.jiantong.work.enums.DocumentsDeleteFlag;
import com.jiantong.work.impl.dao.BreedReportSurveyRecordDao;
import com.jiantong.work.model.BreedReportSurveyRecord;
import com.jiantong.work.service.BreedReportSurveyRecordReadService;
import io.terminus.boot.rpc.common.annotation.RpcProvider;
import io.terminus.common.model.Response;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;


/**
 * Created by niufei on 2017/3/15.
 */
@Slf4j
@Service
@RpcProvider
public class BreedReportSurveyRecordReadServiceImpl implements BreedReportSurveyRecordReadService {

    private final BreedReportSurveyRecordDao breedReportSurveyRecordDao;

    @Autowired
    public BreedReportSurveyRecordReadServiceImpl(BreedReportSurveyRecordDao breedReportSurveyRecordDao) {
        this.breedReportSurveyRecordDao = breedReportSurveyRecordDao;
    }

    @Override
    public Response<BreedReportSurveyRecord> findByReportId(Long reportId) {
        try {
            BreedReportSurveyRecord breedReportSurveyRecord = breedReportSurveyRecordDao.findByReportId(reportId);
            if (breedReportSurveyRecord == null) {
                log.error("breedReportSurveyRecord(id={}) is not found", reportId);
                return Response.fail("breedReportSurveyRecord.not.found");
            }
            return Response.ok(breedReportSurveyRecord);
        } catch (Exception e) {
            log.error("failed to find breedReportSurveyRecord(id={}), cause:{}", reportId, Throwables.getStackTraceAsString(e));
            return Response.fail("breedReportSurveyRecord.find.fail");
        }
    }

    @Override
    public Long findIdByReportId(Long reportId) {
        try {
            return breedReportSurveyRecordDao.findIdByReportId(reportId);
        } catch (Exception e) {
            log.error("failed to find breedReportSurveyRecord id(reportId = {}),cause{}", reportId, Throwables.getStackTraceAsString(e));
            return null;
        }
    }

    @Override
    public Response<List<BreedReportSurveyRecord>> findByNameOrNumber(Map criteria) {
        try {
            return Response.ok(breedReportSurveyRecordDao.findByNameOrNumber(criteria));
        } catch (Exception e) {
            log.error("failed to find breedReportSurveyRecord id(reportId = {}),cause{}", Throwables.getStackTraceAsString(e));
            return null;
        }
    }

    /**
     * 根据检疫证号溯源
     *
     * @param quarantineCertNumber 检疫证号
     * @return BreedReportSurveyRecord
     */
    @Override
    public Response<BreedReportSurveyRecord> findByQuarantineCertNumber(String quarantineCertNumber) {
        try{
            Map<String , Object> criteria = Maps.newHashMap();
            criteria.put("quarantineCertNumber",quarantineCertNumber);
            criteria.put("deleteFlag", DocumentsDeleteFlag.NORMAL.value());
            return Response.ok(breedReportSurveyRecordDao.findByQuarantineCertNumber(criteria));
        }catch (Exception e){
            log.error("failed to find breedReportSurveyRecord cause:{}", Throwables.getStackTraceAsString(e));
            return Response.fail("breedReportSurveyRecord.find.fail");
        }
    }

}
